<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mgpeople extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }    
    
    public function dbincentives($number = 0,$offset = 0)
    {
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT ic.id,ic.title,ic.image,ic.summary,ic.admin_id,ic.active,ic.type,ic.type_sl,ic.province_id,u.username,ic.is_partner FROM incentive_restaurant AS ic LEFT JOIN user AS u ON u.id = ic.admin_id WHERE 1=1 ";
        
        $keyword = $this->getParamString('keyword');
        if(!in_array($keyword, array(null, ''))){
            $sql .= "AND ic.`title` LIKE '%".$keyword."%' ";
        }
        $sql .= "ORDER BY ic.orderby ASC";
        
        $count = $this->db->query($sql)->num_rows();
        if($offset >= $count && $count != 0){
            $this->load->view('template/404',$this->data);
            return false;
        }
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        foreach($xs AS $key => $value){
            if($value->type_sl == 1){
                $restaurants = $this->db->query("SELECT r.name FROM restaurant AS r LEFT JOIN incentive_restaurant_key AS irk ON irk.id_restaurant = r.id WHERE irk.id_incentive = ".$value->id)->result_object();
                $xs[$key]->restaurants = $restaurants;
            }
            if($value->province_id == 1){
                $province = $this->db->query("SELECT vp.name FROM vn_province AS vp LEFT JOIN incentive_province_key AS ipk ON ipk.id_province = vp.provinceid WHERE ipk.id_incentive = ".$value->id)->result_object();
                $xs[$key]->province = $province;
            }
        }
        return array('data' => $xs, 'count' => $count);
    }
    
    private function remove_null_tags($st){
		$Char 	= '<p> </p>|<p>&nbsp;</p>|<p></p>|<span> </span>|<span>&nbsp;</span>|<span></span>|<div> </div>|<div>&nbsp;</div>|<div></div>';
		$explChar 	= explode("|", $Char);
		return str_replace($explChar, '', $st);
	}
    public function dbaddincentives($title,$img,$summary,$content,$type,$month,$year,$admin_id,$type_sl,$restaurant,$img1,$ecoupon,$province_apply,$province,$orderby)
    {
        $post = strip_tags($content,"<a><br>");
        $title_ascii = $this->get_ascii($title);
        $data = array(
            'title' => $title,
            'title_ascii' => $title_ascii,
            'image' => $img,
            'slide_image' => $img1,
            'summary' => $summary,
            'content' => '<div style="font-family: OpenSans;">'.str_replace('<br/>', '', nl2br($this->remove_null_tags($post))).'</div>',
            'ecoupon' => $ecoupon,
            'province_id' => $province_apply,
            'type' => $type,
            'admin_id' => $admin_id,
            'type_sl' => $type_sl,
            'created' => date("Y-m-d H:i:s"),
			'active'	=> $this->getParamInt('active'),
			'is_partner' => $this->getParamInt('is_partner'),
			'orderby' => $orderby
        );
        
        if(!in_array($month, array(null, '', '0')))
        {
            $data['month'] = $month;
        }
        if(!in_array($year, array(null, '', '0')))
        {
            $data['year'] = $year;
        }
        
        //echo '<pre>';
        //die(print_r($data));
        
        $this->db->insert('incentive_restaurant', $data);
        $id = $this->db->insert_id();
        if($type_sl == 1)
        {
            $this->dbaddincentiveskey($id,$restaurant,$admin_id);
        }
        if($province_apply == 1)
        {
            $this->dbaddprovincekey($id,$province,$admin_id);
        }
        return 200;
    }
    
    private function dbaddincentiveskey($id,$restaurant,$admin_id)
    {
        foreach($restaurant AS $key=>$value)
        {
            $data = array(
                'id_restaurant' => $value,
                'id_incentive' => $id,
                'admin_id' => $admin_id,
                'created' => date("Y-m-d H:i:s")
            );
        
            $this->db->insert('incentive_restaurant_key', $data);
        }
        
    }
    private function dbaddprovincekey($id,$province,$admin_id)
    {
        foreach($province AS $key=>$value)
        {
            $data = array(
                'id_province' => $value,
                'id_incentive' => $id,
                'admin_id' => $admin_id,
                'created' => date("Y-m-d H:i:s")
            );
        
            $this->db->insert('incentive_province_key', $data);
        }
    }
    
    public function dbgetrestaurant()
    {
        $sql = "SELECT id,name FROM restaurant";
        $data = $this->db->query($sql)->result_array();
        return $data;
    }
    
    public function dbeditincentives($id,$title,$img,$summary,$content,$type,$month,$year,$admin_id,$type_sl,$restaurant,$img1,$ecoupon,$province_apply,$province,$orderby)
    {
        $post = strip_tags($content,"<a><br>");
        $title_ascii = $this->get_ascii($title);
        $data = array(
            'title' => $title,
            'title_ascii' => $title_ascii,
            'summary' => $summary,
            'content' => '<div style="font-family: OpenSans;">'.str_replace('<br/>', '', nl2br($this->remove_null_tags($post))).'</div>',
            'ecoupon' => $ecoupon,
            'province_id' => $province_apply,
            'type' => $type,
            'admin_id' => $admin_id,
            'type_sl' => $type_sl,
            'created' => date("Y-m-d H:i:s"),
			'active'	=> $this->getParamInt('active'),
			'is_partner'	=> $this->getParamInt('is_partner'),
			'orderby' => $orderby
        );
        if(!in_array($img, array(null, '', '0')))
        {
            $data['image'] = $img;
        }
        if(!in_array($img1, array(null, '', '0')))
        {
            $data['slide_image'] = $img1;
        }
        if($type == 1)
        {
            $data['month'] = $month;
            $data['year'] = $year;
        }else{
            $data['month'] = NULL;
            $data['year'] = NULL;
        }
        
        //echo '<pre>';
        //die(print_r($data));
        
        $this->db->update('incentive_restaurant', $data, "id = '".$id."'");
        
        if($type_sl == 1)
        {    
            $this->dbeditincentiveskey($id,$restaurant,$admin_id);
        }else{
            $this->dbdeleteincentiveskey($id,$restaurant,$admin_id);
        }
        if($province_apply == 1)
        {
            $this->dbeditprovincekey($id,$province,$admin_id);
        }else{
            $this->dbdeleteprovincekey($id,$province,$admin_id);
        }
        return 200;
    }
    private function dbeditincentiveskey($id,$restaurant,$admin_id)
    {
        $this->db->delete('incentive_restaurant_key', array('id_incentive' => $id));
        foreach($restaurant AS $key=>$value)
        {
            $data = array(
                'id_restaurant' => $value,
                'id_incentive' => $id,
                'admin_id' => $admin_id,
                'created' => date("Y-m-d H:i:s")
            );
        
            $this->db->insert('incentive_restaurant_key', $data);
        }
    }
    private function dbdeleteincentiveskey($id)
    {
        $this->db->delete('incentive_restaurant_key', array('id_incentive' => $id));
    }
    
    private function dbeditprovincekey($id,$province,$admin_id)
    {
        $this->db->delete('incentive_province_key', array('id_incentive' => $id));
        foreach($province AS $key=>$value)
        {
            $data = array(
                'id_province' => $value,
                'id_incentive' => $id,
                'admin_id' => $admin_id,
                'created' => date("Y-m-d H:i:s")
            );
            
            $this->db->insert('incentive_province_key', $data);
        }
    }
    private function dbdeleteprovincekey($id)
    {
        $this->db->delete('incentive_province_key', array('id_incentive' => $id));
    }
    public function dbgetinfoincentives($id)
    {
        $query = $this->db->query('SELECT * FROM incentive_restaurant WHERE id = "'.$id.'"');
        if($query->num_rows() > 0)
        {
            $data = $query->row_array();
            $data['id_restaurant'] = $this->db->query('SELECT id_restaurant AS id FROM incentive_restaurant_key WHERE id_incentive = '.$id)->result_array();
            $data['id_province'] = $this->db->query('SELECT id_province AS id FROM incentive_province_key WHERE id_incentive = '.$id)->result_array();
            return $data;
        }else{
            redirect('manage/g-people/incentives'); 
        }
    }    
    
    public function dbactiveincentives($id)
    {
        $de_active = array('active' => 0);
        $active = array('active' => 1);
        $data = $this->db->query("SELECT active FROM incentive_restaurant WHERE id = '".$id."'")->row_array();
        if($data['active'] == 0)
        {
            $this->db->update('incentive_restaurant', $active, "id = '".$id."'");
        }else{
            $this->db->update('incentive_restaurant', $de_active, "id = '".$id."'");
        }
        return 200;
    }
    
    public function dbdeleteincentives($id)
    {
        $path = $this->db->query("SELECT image AS path_0 FROM incentive_restaurant WHERE id = '".$id."'")->row_array();
        $slide = $this->db->query("SELECT slide_image FROM incentive_restaurant WHERE id = '".$id."'")->row_array();
        $slides = json_decode($slide['slide_image']);
        $i = 0;
        foreach($slides AS $key => $value)
        {
            $i = $i + 1;
            $path['path_'.$i] = $value;
        }
        //echo '<pre>';
        //die(print_r($path));
        
        foreach($path AS $key => $value)
        {
            @unlink(PROJECT_PATH.'/data/'.$value);
        }
        
        $this->db->delete('incentive_restaurant', array('id' => $id));
        $this->db->delete('incentive_restaurant_key', array('id_incentive' => $id));
        $this->db->delete('incentive_province_key', array('id_incentive' => $id));
        return 200;
    }
    
    public function selecteCoupon()
    {
        $sql = "SELECT id,title AS name FROM ecoupon";
        $data = $this->db->query($sql)->result_array();
        return $data;
    }
    
    public function listProvince()
    {
        $sql = "SELECT provinceid AS id,name FROM vn_province";
        $data = $this->db->query($sql)->result_array();
        return $data;
    }
}
